<?php


class User_IndexController extends Engine_Controller_Action {
	
	
	public function indexAction() {
		if ($this->auth->isActive()) {
			$data = $this->auth->authRecord();
			echo $data->user_email . '<br/>';
			echo $data->user_username . '<br/>';
		} else {
			$this->view->message = $this->translate->_('ENGINE_LOGIN_REQUIRED');
		}
	}
	
	
	public function loginAction() {
	
		// Check if user is already logged in
		if (!$this->auth->isActive()) {
			
			// Create the login form
			if (!$this->getRequest()->isPost()) {			
				// Load the form into the view				
				$this->view->loginForm = new User_Form_Login($this->_getAllParams());
				
			} else {				
				// Validate the form data
				$form = new User_Form_Login();
				if (!$form->isValid($_POST)) {
					
					// Failed input validation
					echo $form;
					
				} else {				
					
					// Acquire the form data and authenticate
					$values = $form->getValues();
					$account = new Engine_User_Account();
					$result = $account->login($values);
					// $result = $this->auth->authenticate($values);
					
					// Determine the login results and route accordingly
					switch ($result) {
						
						// Login AUTH Failure
						case AUTH_RESULT_INVALID_LOGIN:
							$this->view->message = $this->translate->_('ERRORS_LOGIN_FAILED')."<br/>";
							$this->view->loginForm = new User_Form_Login($this->_getAllParams());
							break;
							
						case AUTH_RESULT_SUSPENDED:
							$this->view->message = "Account suspended.";
							$path = "/index/inactive.phtml";
							$this->renderScript($path);
							break;
							
						case AUTH_RESULT_INACTIVE:
							$this->view->message = "Account inactive.";
							$path = "/index/inactive.phtml";
							$this->renderScript($path);
							break;

						// Login SUCCESS
						case AUTH_RESULT_SUCCESS:			

							// Redirect the Login to the referer page or the user page
							if (isset($this->session->pageRequest)) {
								$uri = $this->session->pageRequest;
								unset($this->session->pageRequest);
								$this->_redirect(Zend_Registry::get('baseUrl').$uri);	
							} else {
								$this->_redirect(Zend_Registry::get('baseUrl').'/user');
							}
							break;
							
						// Unknown ERROR
						default :
							$this->view->message = $this->translate->_('ERRORS_UNKNOWN_ERROR')."<br/>";
							$this->view->loginForm = new User_Form_Login();
							break;
					}
				}
			}
		} else {
			
			// LOGGED ON
			// Already logged in, so get Zendauth record and offer logoff options
			$data = $this->auth->authRecord();
			$this->view->message = sprintf($this->translate->_('ENGINE_LOGGED_ON_AS'), $data->user_username) . "<br/>
						<a href='" . Zend_Registry::get('baseUrl') . "/user/logout'>Logout</a>";
			
			
			$path = "/index/logout.phtml";
			$this->renderScript($path);
		}
	}
	
	
	public function logoutAction() {	
		$account = new Engine_User_Account();
		$account->logout();
		// $this->_redirect(Zend_Registry::get('baseUrl'));
	}	
	
	
	public function profileAction() {
		
		// Get the dynamic username from the URL
		$username = $this->getRequest()->getParam('username');

		// If username, the validate username
		if (isset($username)) {
			// If the username is not a valid username, return a 404 Page not found
			$account = new Engine_User_Account();
			if (!$account->validUsername($username)) {
				$this->throwError(404);
			} else {
				// If the username is found, then prepare to load username details.
				$loadProfile = true;
			}
			
		// No username provided
		} else {
			// If there authenticated user, prepate to load their profile
			if ($this->auth->isActive()) {
				$authRecord = $this->auth->authRecord();
				$username = $authRecord->user_username;
				$loadProfile = true;
			}
		}
		
		if ($loadProfile) {
			echo $this->view->navigation()->breadcrumbs();
		} else {
			$this->_redirect(Zend_Registry::get('baseUrl'). '/user/login');
		}
	}
	
	public function registerAction() {
	
		if (!$this->auth->isActive()) {
			
			if (isset($this->session->pageRequest)) {	
				unset($this->session->pageRequest);	
			}	
			
			if (!$this->getRequest()->isPost()) {			
				// Load the form into the view				
				$this->view->registerForm = new User_Form_Register($this->_getAllParams());
							
			} else {

				$form = new User_Form_Register();
				if (!$form->isValid($_POST)) {	
					
					// Failed input validation
					$this->view->registerForm = $form;
					
				} else {
					
					// Create new account
					$values = $form->getValues();
					$account = new Engine_User_Account();

					if (!$account->create($values)) {
						
						// Failed to create account
						$this->view->message = '<p>'.$account->getError().'</p>';
						$this->view->registerForm = $form;

					} else {

						// Success - Provide "Email Validation" message	
						$this->_redirect(Zend_Registry::get('baseUrl').'/user/register/verify');
					}
				}
			}
		} else {
			// Already logged in
		}
	}
	
	public function verifyAction() {
		$code = $this->_getParam('code');
		
		if (isset($code)) {
			$account = new Engine_User_Account();
			if ($account->verifyCode($code)) {
				// Email verified and Account set to Membership
			} else {
				// Verification fales
			}
		}
	}
	
	public function unauthorizedAction() {
		if (isset($this->session->pageRequest)) {	
			$this->view->pageRequest = $this->session->pageRequest;
			unset($this->session->pageRequest);	
		}	
	}
	
	public function inactiveAction() {
		
	}
	
}
